<template>
  <j-modal
    :confirmLoading="confirmLoading"
    :dialogStyle="dialogStyle"
    :title="title"
    :visible="visible"
    :width="500"
    cancelText="关闭"
    switchFullscreen
    @cancel="handleCancel"
    @ok="handleOk"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="城市">
          <a-input
            v-decorator="[
              'cityName',
              {
                rules: [
                  {
                    required: true,
                    message: '城市不能为空!',
                  },
                ],
              },
            ]"
            placeholder="请输入城市"
          />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="地区范围">
          <a-select
            v-decorator="[
              'type',
              {
                rules: [
                  {
                    required: true,
                    message: '范围不能为空!',
                  },
                ],
              },
            ]"
            placeholder="请选择地区范围"
            @change="handleChange"
          >
            <a-select-option value="0"> 国内 </a-select-option>
            <a-select-option value="1"> 国际 </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="数据类型">
          <a-select
            v-decorator="[
              'isDlpf',
              {
                rules: [
                  {
                    required: true,
                    message: '范围不能为空!',
                  },
                ],
              },
            ]"
            placeholder="请选择数据类型"
            @change="handleChange"
          >
            <a-select-option value="0"> 碳强度 </a-select-option>
            <a-select-option value="1"> 电力因子 </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="碳强度">
          <a-input
            v-decorator="[
              'carbonNum',
              {
                rules: [
                  {
                    required: true,
                    message: '仅能输入数字和两位小数，不能为空或为负数！',
                    pattern: /^[0-9]+\.?[0-9]{0,2}$/,
                  },
                ],
              },
            ]"
            placeholder="请输入碳强度"
            suffix="吨CO₂/万元"
          />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol2" label="是否展示">
          <!--          <a-select v-decorator="['status', {initialValue: defaultValue,-->
          <!--            rules: [-->
          <!--              {-->
          <!--                required: true,-->
          <!--                message: '不能为空!',-->
          <!--              },-->
          <!--            ],-->
          <!--            }]" placeholder="请选择是否在前台展示" @change="handleChange" >-->
          <!--          <a-select-option value="0">-->
          <!--            是-->
          <!--          </a-select-option>-->
          <!--          <a-select-option value="1">-->
          <!--            否-->
          <!--          </a-select-option>-->
          <!--        </a-select>-->
          <a-radio-group
            v-decorator="[
              'status',
              {
                initialValue: defaultValue,
                rules: [
                  {
                    required: true,
                    message: '不能为空!',
                  },
                ],
              },
            ]"
          >
            <!--                <a-radio value="">所有</a-radio>-->
            <a-radio value="1" style="margin-right: 40px">是</a-radio>
            <a-radio value="0">否</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import pick from 'lodash.pick'
import { postAction } from '@api/manage'

export default {
  name: 'analysisModal',
  data() {
    return {
      confirmLoading: false,
      title: '',
      visible: false,
      form: this.$form.createForm(this),
      dialogStyle: {
        top: '300px',
        // right:"20px",
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      wrapperCol2: {
        xs: { span: 24, offset: 1 },
        sm: { span: 10 },
      },
      validatorRules: {
        totalEnergyConsumption: {
          rules: [{ required: true, trigger: 'change', message: '请输入数字！', pattern: '^\d{1,}\.\d{1,}|\d{1,}]/g' }],
        },
      },
      defaultValue: '0',
      url: {
        add: '/carbon/carbonOverviewComparativeAnalysis/add',
        edit: '/carbon/carbonOverviewComparativeAnalysis/edit',
      },
    }
  },
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      console.log(record)
      this.visible = true
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'cityName',
            'type',
            'carbonNum',
            'status',
            'isDlpf',
            // 'year',
            // 'totalEnergyConsumption',
            // 'totalEnergyConsumptionYoy',
            // 'totalCarbonEmissions',
            // 'totalCarbonEmissionsYoy',
            // 'energyConsumptionIntensity',
            // 'energyConsumptionIntensityYoy',
            // 'carbonEmissionIntensity',
            // 'carbonEmissionIntensityYoy'
          ),
        )
        // year: this.model.batteryyearNumber,
      })
    },
    handleChange(value) {
      console.log(value)
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    handleOk() {
      let that = this
      this.form.validateFields((err, values) => {
        if (!err) {
          let httpurl = ''
          this.confirmLoading = true
          console.log(this.model)
          if (!this.model.id) {
            httpurl += this.url.add
          } else {
            httpurl += this.url.edit
          }
          console.log(httpurl)
          let formData = Object.assign(this.model, values)
          postAction(httpurl, formData)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('refresh')
              } else {
                that.$message.error(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
              that.$parent.$parent.loadData()
            })
        }
      })
    },
  },
}
</script>

<style scoped></style>
